package bpel2rep.version.impl;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.junit.Before;
import org.junit.Test;

import bpel2rep.version.MetaInfo;
import bpel2rep.version.StateName;
import bpel2rep.version.StatusName;
import bpel2rep.version.Version;
import bpel2rep.version.VersionFactory;
import bpel2rep.version.VersionId;
import bpel2rep.version.VersionStore;
import bpel2rep.version.WorkingStatus;
import bpel2rep.version.store.StoreFactory;
import bpel2rep.version.store.TeneoStore;

public class VersionImplTest {
	private TeneoStore ts;
	private VersionFactory vf;
	
	@Before
	public void setUp() throws Exception {
		bpel2rep.version.store.StoreFactory f = bpel2rep.version.store.StoreFactory.eINSTANCE;
		this.ts = f.createTeneoStore();
		this.vf = bpel2rep.version.VersionFactory.eINSTANCE;
	}
	@Test
	public void testCheckin() {
		//create a new version
		MetaInfo info = vf.createMetaInfo();
		info.setDescription("this is 1. test");
		info.setId("tiw_1");
		info.setName("tiw_test");
		// get a TProcess
		String xmlFile = "sampleprocesses/HelloWorld2.bpel";
		executable.util.ExecutableXMLProcessor processor = null;
		Resource resource = null;
		try {
			processor = new executable.util.ExecutableXMLProcessor();
			resource = processor
					.load(new File(xmlFile).getAbsolutePath(), null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		EList<EObject> eos = resource.getContents();
		executable.impl.DocumentRootImpl dri = (executable.impl.DocumentRootImpl) eos
				.get(0);
		executable.TProcess tp = dri.getProcess();

		Version v = vf.initVersion(tp, info);
		
		
		assertTrue("the state of initial version before checkin is: " + v.getStateName(), v.getStateName() == StateName.INITIAL);
		WorkingStatus ws = VersionFactory.eINSTANCE.createWorkingStatus();
		v.checkin(v, true,ws);
		assertTrue("the working status is: " + ws.getStatusName().toString(), ws.getStatusName() == StatusName.NORMAL);
		assertTrue("the state of initial version after checkin is: " + v.getStateName(), v.getStateName() == StateName.FREE);
		assertTrue("the major id after check is: " + v.getVersionId().getMajorID(), v.getVersionId().getMajorID() == 1);
		
		VersionId vid = VersionFactory.eINSTANCE.createVersionId();
		vid.setMajorID(1);
		vid.setMinorID(0);
		bpel2rep.version.Version v2 = null;
		try {
			v2 = ts.retrieve(v.getProcess().getId(), vid);
			assertTrue ("name of metainfo is:" + v2.getMetaInfo().getName(), v2.getMetaInfo().getName().equals("tiw_test"));
			assertTrue("state name is: " + v2.getStateName().toString(), v2.getStateName() == StateName.FREE);
//			assertTrue("name of state is: " + v2.getVersionState().getName(), v2.getVersionState().getName() == "FreeImpl");
			// SHOULD BE bpel2rep.version.state.impl.FreeImpl but is VersionStateImpl
			//assertTrue ("state of version is: " + v2.getVersionState().getClass().getName(), v2.getVersionState().getClass() == FreeImpl.class);
			
			// create resource set and resource
			ResourceSet resourceSet = new ResourceSetImpl();

			// Register XML resource factory
			resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
					.put("xmi", new XMIResourceFactoryImpl());

			Resource resourceVersion = resourceSet.createResource(URI
					.createFileURI("tmp/version2.xmi"));
			// add the root object to the resource
			resourceVersion.getContents().add(v2);
			try {
				resourceVersion.save(null);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testCheckout() {
		//2a4b25cb-014a-4544-9d14-e5b73f99378d
		//versionId 1 0
		VersionId vid = VersionFactory.eINSTANCE.createVersionId();
		vid.setMajorID(1);
		vid.setMinorID(0);
		VersionStore vs = StoreFactory.eINSTANCE.createTeneoStore();
		WorkingStatus ws = VersionFactory.eINSTANCE.createWorkingStatus();
		bpel2rep.version.Version v2 = VersionFactory.eINSTANCE.createVersion(vs);
			try {
				assertNotNull(v2.checkout("74d6fd04-8a93-4516-96fe-de1a2ed76d21", vid, ws));
				assertTrue("the state name of v2 is: " + v2.getStateName().toString(),v2.getStateName() == StateName.LOCK_WORK);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

}
